﻿using System;
namespace Jumbot.API.Discuz
{
    public class SqlServerDbCom : IDbCommand
    {
        public bool Register(string username, int gender, string nickname, string password, string email, int groupid, string regip, string qq, string msn)
        {
            if (username.Length == 0 || password.Length == 0) return false;
            string Sql = String.Format("select top 1 [uid] from [{1}users] where [username]='{0}'", username, Command.RC.Tableprefix);

            System.Data.DataTable myDt = Command.DBC.vDataTable(Sql);
            if (Object.Equals(myDt, null) || myDt.Rows.Count <= 0)
            {

                object[,] parsBBS;
                if (password.Length == 32)//表示密文
                {
                    parsBBS = new object[2, 7] { 
            { "@username","@gender","@nickname","@password","@groupid","@email","@regip" }, 
            { username, gender, nickname, password, groupid, email, regip } };
                }
                else
                {
                    parsBBS = new object[2, 7] { 
            { "@username","@gender","@nickname","@password","@groupid","@email","@regip" }, 
            { username, gender, nickname, Command.Md5(password), groupid, email, regip } };
                }
                int LastId;
                if (Command.DBC.vExecuteNonQuery(parsBBS,
                    String.Format("insert into [{0}users] ([username],[gender],[nickname],[password],[groupid],[email],[regip],[credits],[extcredits1],[extcredits2]) values (@username,@gender,@nickname,@password,@groupid,@email,@regip," + Command.RC.DefaultExtcredits1 + "," + Command.RC.DefaultExtcredits1 + "," + Command.RC.DefaultExtcredits2 + ")", Command.RC.Tableprefix), false, out LastId) <= 0)
                {
                    return false;
                }
                object lid;
                if (LastId <= 0)
                {
                    lid = Command.DBC.vExecuteScalar(String.Format("select top 1 [uid] from [{0}users] order by uid desc", Command.RC.Tableprefix));
                }
                else
                {
                    lid = LastId;
                }

                parsBBS = new object[2, 3] { 
            { "@uid", "@qq", "@msn" }, 
            { lid, qq, msn } };
                if (Command.DBC.vExecuteNonQuery(parsBBS, String.Format("insert into [{0}userfields] ([uid], [qq], [msn]) values (@uid, @qq, @msn)", Command.RC.Tableprefix), false) <= 0)
                {
                    Command.DBC.vExecuteNonQuery(String.Format("delete from [{0}users] where [uid]={1}", Command.RC.Tableprefix, LastId));
                    return false;
                }

                parsBBS = new object[2, 2] { { "@username", "@userid" }, { username, lid } };
                Command.DBC.vExecuteNonQuery(parsBBS, String.Format("update [{0}statistics] set totalusers=totalusers+1, lastusername=@username, lastuserid=@userid", Command.RC.Tableprefix), false);
                return true;
            }
            else
                return false;
        }

        public bool ModifyPwd(string username, string password)
        {
            if (password.Length == 32)//表示密文
                return (Command.DBC.vExecuteNonQuery(String.Format("update [{0}users] set [password]='{2}' where [username]='{1}' and [password]<>'{2}'", Command.RC.Tableprefix, username, password)) == 1);
            else
                return (Command.DBC.vExecuteNonQuery(String.Format("update [{0}users] set [password]='{2}' where [username]='{1}' and [password]<>'{2}'", Command.RC.Tableprefix, username, Command.Md5(password))) == 1);
        }

        public virtual bool Login(string username, string password, int expDays, bool inVisible, string reFerer)
        {
            string Sql;
            if (password.Length == 32)//表示密文
                Sql = String.Format("select top 1 [tpp],[ppp],[pmsound],[invisible],[sigstatus],[uid] from [{2}users] where [username]='{0}' and [password]='{1}'", username, password, Command.RC.Tableprefix);
            else
                Sql = String.Format("select top 1 [tpp],[ppp],[pmsound],[invisible],[sigstatus],[uid] from [{2}users] where [username]='{0}' and [password]='{1}'", username, Command.Md5(password), Command.RC.Tableprefix);

            System.Data.DataTable myDt = Command.DBC.vDataTable(Sql);
            if (Object.Equals(myDt, null) || myDt.Rows.Count <= 0)
            {
                Command.Register(username, 0, String.Empty, password, String.Empty);
                myDt = Command.DBC.vDataTable(Sql);
            }
            GetUserInfo userinfo = new GetUserInfo(myDt);
            if (Object.Equals(myDt, null) || myDt.Rows.Count <= 0) return false;

            Command.DBC.vExecuteNonQuery(String.Format("UPDATE [{0}users] SET [lastvisit]=GetDate(), [lastip]='{2}' WHERE [username]='{1}'", Command.RC.Tableprefix, username, Command.RC.UserIP));

            return Command.WriteLoginCookie(userinfo, inVisible, reFerer, expDays, password);

        }

        public virtual void LoginOut()
        {
            try
            {
                System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["dnt"];
                if (Object.Equals(cookie, null))
                {
                    cookie = new System.Web.HttpCookie("dnt");
                }
                else
                {
                    if (!Object.Equals(cookie.Values["userid"], null))
                    {
                        string Uid = cookie.Values["userid"].ToString();
                        if (!String.IsNullOrEmpty(Uid))
                        {
                            int uid;
                            Int32.TryParse(Uid, out uid);
                            Command.DBC.vExecuteNonQuery(String.Format("UPDATE [{0}users] SET [oltime] = [oltime] + DATEDIFF(n,[lastvisit],GetDate()) WHERE [uid]={1};delete from [{0}online] where userid={1};", Command.RC.Tableprefix, uid));
                        }
                    }
                }
                cookie.Values.Clear();
                cookie.Expires = DateTime.Now.AddYears(-1);
                if (Command.IsValidDomain(Command.RC.doMain))
                {
                    cookie.Domain = Command.RC.doMain;
                }
                System.Web.HttpContext.Current.Response.AppendCookie(cookie);
            }
            catch
            { }
        }
    }
}
